package com.hymall.controller.admin;

import com.hymall.bean.MarketTopic;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.BaseRespVo;
import com.hymall.bean.common.CommonData;
import com.hymall.service.admin.AdminTopicService;
import com.hymall.service.admin.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 *
 * @author 赵思源
 * @since 2022/07/19 02:08
 * @description: 推广管理 -> 专题管理 http://182.92.235.201:8083/admin/topic/list?page=1&limit=20&sort=add_time&order=desc
 */
@RestController
@RequestMapping("admin/topic")
public class AdminTopicController {

    @Autowired
    AdminTopicService adminTopicService;
    // GoodsService goodsService;

    /**
     *
     * @author 赵思源
     * @date 2022/7/19 10:11 显示专题管理页面
     */

    @GetMapping("list")
    public BaseRespVo list(String title, String subtitle, BasePageInfo info) {

        CommonData data = adminTopicService.queryTopicList(title, subtitle, info);
        return BaseRespVo.ok(data);
    }

    /**
     * @author Zhaosiyuan
     * @date 2022/7/19 14:02 增加专题活动
     * 要求：价格>0
     * 老师没实现的BigDecimal判断价格------->已完成
     * TODO:涉及商品和图片的功能的未添加
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketTopic topic) {
        if (TopicJudGoodPrice(topic)) {
            return BaseRespVo.error(507,"价格需要大于0");
        }
        return adminTopicService.creat(topic);
    }

    /**
     * 修改专题活动
     * @author Zhaosiyuan
     * @date 2022/7/20  10:13
     * TODO:涉及商品和图片的功能的未添加
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketTopic topic){
        if (TopicJudGoodPrice(topic)) {

            return BaseRespVo.error(507,"价格需要大于0");
        }
        return adminTopicService.updateTopic(topic);
    }

    /**
     * $ 删除专题活动
     * @author Zhaosiyuan
     * @date 2022/7/20  10:15
     * {"errno":0,"errmsg":"成功"}
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketTopic topic){
        int code = adminTopicService.delete(topic);
        if(code == 200){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData("删除失败");
    }

    /**
     * 批量删除专题活动
     * @author Zhaosiyuan
     * @date 2022/7/20  15:35
     *
     */
    @PostMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody Map<String,Integer[]> map){
        Integer[] ids = map.get("ids");
        int code = adminTopicService.batchDelete(ids);
        if(code == 200){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData("删除失败");
    }

    /**
     * 编辑专题活动
     * @author Zhaosiyuan
     * @date 2022/7/20  15:52
     * TODO:涉及商品和图片的功能的未添加
     */
    @GetMapping("read")
    public BaseRespVo read(Integer id){
        MarketTopic marketTopic;
        marketTopic = adminTopicService.selectTopicById(id);
        if(marketTopic == null){
            return BaseRespVo.invalidData("未找到该活动");
        }
        Integer[] goods = marketTopic.getGoods();
        return BaseRespVo.ok(goods,marketTopic);
    }
    /**
     * 判断活动定价是否合理的方法
     * @author Zhaosiyuan
     * @date 2022/7/20 18:26
     *
     */
    private boolean TopicJudGoodPrice(@RequestBody MarketTopic topic) {
        BigDecimal bigDecimal = new BigDecimal(0);
        int flag = topic.getPrice().compareTo(bigDecimal);
        if (flag != -1) {
            return false;
        }
        return true;
    }
}
